package com.zdsoft.finance.meet.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.meet.entity.Meeting;
import com.zdsoft.finance.meet.entity.MeetingMember;
import com.zdsoft.finance.meet.entity.MeetingProject;
import com.zdsoft.finance.meet.repository.MeetingMemberRepository;
import com.zdsoft.finance.meet.repository.MeetingProjectRepository;
import com.zdsoft.finance.meet.repository.MeetingRepository;
import com.zdsoft.finance.meet.service.MeetingProjectService;
import com.zdsoft.finance.spi.meeting.dto.MeetingProjectStatus;
import com.zdsoft.finance.spi.meeting.dto.MeetingStatus;
import com.zdsoft.finance.spi.meeting.dto.MeetingVoteResult;
import com.zdsoft.framework.core.common.page.Page;
import com.zdsoft.framework.core.common.page.Pageable;
import com.zdsoft.framework.core.common.util.DateHelper;
import com.zdsoft.framework.core.common.util.ObjectHelper;

@Service("meetingProjectService")
public class MeetingProjectServiceImpl extends BaseServiceImpl<MeetingProject, CustomRepository<MeetingProject, String>>
		implements MeetingProjectService {

	@Autowired
	MeetingProjectRepository meetingProjectRepository;

	@Autowired
	MeetingMemberRepository meetingMemberRepository;

	@Autowired
	MeetingRepository meetingRepository;

	@Override
	public List<MeetingProject> findByIds(List<String> ids) {
		return meetingProjectRepository.findByIdIn(ids);
	}

	@Override
	public Page<MeetingProject> findByMeetingId(Pageable pageable, String meetingId) {
		org.springframework.data.domain.Pageable pageJpa = new PageRequest(pageable.getPage() - 1, pageable.getRows());
		org.springframework.data.domain.Page<MeetingProject> meetingProjects = meetingProjectRepository
				.findByMeetingId(pageJpa, meetingId);
		Page<MeetingProject> meetingProject = new com.zdsoft.framework.core.common.page.PageImpl<>(pageable);
		meetingProject.setRecords(meetingProjects.getContent());
		meetingProject.setTotalRows(meetingProjects.getTotalElements());
		return meetingProject;
	}

	@Override
	public Page<MeetingProject> findByConditions(Pageable pageable, Map<String, Object> conditions) {
		org.springframework.data.domain.Pageable pageJpa = new PageRequest(pageable.getPage() - 1, pageable.getRows());
		org.springframework.data.domain.Page<MeetingProject> meetingProjects = meetingProjectRepository
				.findByConditions(pageJpa, conditions);

		Page<MeetingProject> meetingProject = new com.zdsoft.framework.core.common.page.PageImpl<>(pageable);
		meetingProject.setRecords(meetingProjects.getContent());
		meetingProject.setTotalRows(meetingProjects.getTotalElements());

		return meetingProject;
	}

	@Override
	@Transactional
	public MeetingProject changeProStatus(MeetingProject meetingProject) throws Exception {

		MeetingProject oldMeetingProject = this.findOne(meetingProject.getId());

		// 修改
		if (meetingProject.getState() == MeetingProjectStatus.MEET_PROJECT_STATE_REFUSE.value) {
			oldMeetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_REFUSE.value);
			oldMeetingProject.setRefuseDateTime(DateHelper.dateToLong(new Date(), DateHelper.DATE_LONG_SIMPLE_FORMAT));
			oldMeetingProject.setRefuseReason(meetingProject.getRefuseReason());
			oldMeetingProject.setRefuseEmpId(meetingProject.getRefuseEmpId());
			oldMeetingProject.setRefuseEmpName(meetingProject.getRefuseEmpName());
		} else if (meetingProject.getState() == MeetingProjectStatus.MEET_PROJECT_STATE_TOACCR.value) {
			oldMeetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_TOACCR.value);
			oldMeetingProject.setEndVoteDateTime(DateHelper.dateToLong(new Date(), DateHelper.DATE_LONG_SIMPLE_FORMAT));
			oldMeetingProject.setEndVoteReason(meetingProject.getEndVoteReason());
			oldMeetingProject.setEndvoteEmpId(meetingProject.getEndvoteEmpId());
			oldMeetingProject.setEndVoteEmpName(meetingProject.getEndVoteEmpName());

			List<String> meetingProjectIds = new ArrayList<String>();
			meetingProjectIds.add(oldMeetingProject.getId());

			// 修改未投票的结果为弃权
			List<MeetingMember> meetingMembers = meetingMemberRepository.findByMeetingProjectIds(meetingProjectIds);
			for (int i = 0; i < meetingMembers.size(); i++) {
				// 确认提交为null,或者为false,则直接修改为弃权状态
				if (ObjectHelper.isEmpty(meetingMembers.get(i).getIsSubmit()) || !meetingMembers.get(i).getIsSubmit()) {
					MeetingMember meetingMember = meetingMembers.get(i);
					meetingMember.setVote(MeetingVoteResult.VOTE_WAIVER.value);
					meetingMemberRepository.updateEntity(meetingMember);
				}
			}

			Meeting meeting = oldMeetingProject.getMeeting();
			// 判断整个会议是否结束
			Boolean isEnd = true;
			List<MeetingProject> meetingProjects = meetingProjectRepository.findByMeetingId(meeting.getId());
			for (int i = 0; i < meetingProjects.size(); i++) {

				// 如果上会项目状态为null，或者不为待出纪要状态，那么则会议不能修改为结束状态
				if (ObjectHelper.isEmpty(meetingProjects.get(i).getState()) || (meetingProjects.get(i)
						.getState() == MeetingProjectStatus.MEET_PROJECT_STATE_TOACCR.value)) {
					isEnd = false;
				}
			}

			// 修改会议状态为结束
			if (isEnd) {
				meeting.setState(MeetingStatus.MEET_STATE_FINAL_END.value);
				meetingRepository.updateEntity(meeting);
			}
		}

		return this.updateEntity(oldMeetingProject);
	}

	@Override
	public MeetingProject findByBusinessId(String businessId) {
		return meetingProjectRepository.findByBusinessId(businessId);
	}

}
